package com.dh.blog.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dh.blog.dao.BlogDao;
import com.dh.blog.dao.UserDao;
import com.dh.blog.service.UserService;
import com.dh.blog.utils.Encrypter;
import com.dh.blog.vo.BlogVo;
import com.dh.blog.vo.UserVo;
import com.ewei.web.common.exception.MessageRuntimeException;
import com.mysql.jdbc.StringUtils;

@Service("userService")
@Transactional(readOnly=true)
public class UserServiceImpl implements UserService {
	@Resource
	private UserDao userDao;
	@Resource
	private BlogDao blogDao;
	@Override
	@Transactional(readOnly=false)
	public void doRegister(UserVo user) {
		String path = user.getPath();
		if(!StringUtils.isNullOrEmpty(path)){
			BlogVo newBlog = new BlogVo(path);
			blogDao.addOne(newBlog);
			user.setId(newBlog.getId());//博客id作为用户id
			user.setPassword(Encrypter.encryptPwd(user.getLoginName(),
					user.getPassword(), "shiro"));//密码加密
			userDao.addOne(user);
		}else{
			throw new MessageRuntimeException("the path is null");
		}
	}
	@Override
	public UserVo getByLoginName(String loginName) {
		return userDao.getUserByLoginName(loginName);
	}
	@Override
	public boolean isExistBlog(String path) {
		return	blogDao.getBlogByPath(path)!=null;
	}

}
